/*
 * @Author: your name
 * @Date: 2021-11-11 21:23:50
 * @LastEditTime: 2021-11-11 21:43:23
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \undefinedc:\Users\16860\Desktop\index.js
 */
var osUtils = require("os-utils");
const express = require('express');
const app = express();
var os = require("os");
var interval = -1;
var currCPU = 0;
function getInfo(){
     return new Promise((resolve,reject)=>{
        osUtils.cpuUsage(function (value) {
            currCPU = value;
            var infos = os.networkInterfaces();
            var freeMem = os.freemem() / 1024 / 1024 / 1024;
            var totalMem = os.totalmem() / 1024 / 1024 / 1024;
            var cpus = os.cpus()
            var data = {
                cpuUsage: (currCPU * 100.0).toFixed(2) + "%", //Cpu 使用率
                cpuArch: os.arch(),     // cpu 类型 
                cpuCores: cpus.length,  // cpu 核心数 
                freeMem: freeMem.toFixed(2) + "G",  // 内存空间可用空间
                totalMem: totalMem.toFixed(2) + "G",// 内存空间总空间
                usedMem: (totalMem - freeMem).toFixed(2) + "G", // 已经使用的
                MemUsage: ((totalMem - freeMem) / totalMem * 100.0).toFixed(2) + "%", // 内存空间使用率
                Ip:infos.WLAN[1].address,
                hostName:os.hostname(), // 主机名称
                platform:os.platform(), //操作系统类型
                type:os.type()  //操作系统名称
            };
            if(data){
                resolve(data)
            }else{
                reject(['error']) 
            }
        });
     })
}
app.get('/datas',async (req,res)=>{
    await getInfo().then(data=>{
        res.json(data) 
    })
})
app.listen(3061,()=>{
      console.log('connect')
})


